package com.atguigu.crowd.mvc.handler;

import com.atguigu.crowd.entity.Menu;
import com.atguigu.crowd.service.api.MenuService;
import com.atguigu.crowd.util.ResultEntity;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;
import org.springframework.web.bind.annotation.RestController;

import java.util.HashMap;
import java.util.List;
import java.util.Map;

//@ResponseBody
//@Controller
// @RestController 作用相当于@Controller+@ResponseBody
@RestController
public class MenuHandler {

    @Autowired
    private MenuService menuService;

//    @ResponseBody
    @RequestMapping("/menu/do/remove.json")
    public ResultEntity<Menu> removeMenu(Integer id){

        menuService.removeMenu(id);

        return ResultEntity.successWithoutData();
    }

//    @ResponseBody
    @RequestMapping("/menu/do/edit.json")
    public ResultEntity<Menu> editMenu(Menu menu){

        menuService.edit(menu);

        return ResultEntity.successWithoutData();

    }

//    @ResponseBody
    @RequestMapping("/menu/do/save.json")
    public ResultEntity<Menu> saveMenu(Menu menu){

        menuService.save(menu);

        return ResultEntity.successWithoutData();

    }

//    @ResponseBody
    @RequestMapping("/menu/do/get.json")
    public ResultEntity<Menu> getWholeTree() {

        // 通过service层方法得到全部Menu对象的List
        List<Menu> menuList = menuService.getAll();

        // 声明一个Menu对象root，用来存放找到的根节点
        Menu root = null;

        // 使用map表示每一个菜单与id的对应关系
        Map<Integer, Menu> menuMap = new HashMap<>();

        // 将菜单id与菜单对象以K-V对模式存入map
        for (Menu menu : menuList) {
            menuMap.put(menu.getId(), menu);
        }

        for (Menu menu : menuList) {

            Integer pid = menu.getPid();

            // pid为null表示该menu是根节点
            if (pid == null) {
                root = menu;
                continue;
            }

            // 通过当前遍历的menu的pid得到其父节点
            Menu father = menuMap.get(pid);

            // 为父节点添加子节点为当前节点
            father.getChildren().add(menu);

        }

        // 将根节点作为data返回（返回根节点也就返回了整棵树）
        return ResultEntity.successWithData(root);

    }


}
